package operation;


import com.supermap.desktop.controls.ui.UICommonToolkit;
import com.supermap.desktop.controls.ui.trees.WorkspaceTree;
import robotCross.TipMatcher;
import util.Info;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import java.util.ArrayList;

public class WorkspaceTreeOperation extends TreeOperation {
	ArrayList<TreeNode> treeNodes = new ArrayList<TreeNode>();
	WorkspaceTree tree;

	public WorkspaceTreeOperation(){
		tree = UICommonToolkit.getWorkspaceManager().getWorkspaceTree();
	}
	public WorkspaceTreeOperation(JTree tree){
		this();
		if(tree != null && tree instanceof WorkspaceTree){
			this.tree = (WorkspaceTree) tree;
		}
	}
	public static void closeWorkspace() throws Exception {
		new WorkspaceTreeOperation().selectWorkspaceNodeMenu("关闭工作空间");
		Operation.robot.delay(2000);
		//提示是不一定会弹出的，所以这里的异常是可以接受的
		try {
			new DialogOperation().clickComponent(new TipMatcher("确定"));
		} catch (Exception e) {
//			e.printStackTrace();
		}
		try {
			new DialogOperation().clickComponent(new TipMatcher("否"));
		} catch (Exception e) {
//			e.printStackTrace();
		}
	}
	public void selectPopupMenuItem(String menu) throws Exception {
		if(treeNodes!= null && treeNodes.size()>0){
			TreeOperation.selectPopupMenuItem((DefaultMutableTreeNode) treeNodes.get(treeNodes.size() - 1),menu);
		}
		else {
			Info.out("工作空间树节点未找到" + menu);
			throw new Exception("工作空间树节点未找到:" + menu);
		}
	}

	public ArrayList<TreeNode> selectWorkspaceNode(){
		ArrayList<SearchMap> searchMapList = new ArrayList<SearchMap>();
		//searchMapList为空，默认选中父节点
		treeNodes = TreeOperation.selectTreeNodes(tree,searchMapList);
		return treeNodes;
	}
	public ArrayList<TreeNode> selectWorkspaceNodeMenu(String menu) throws Exception {
		this.selectWorkspaceNode();
//		Operation.robot.delay(3000);
		this.selectPopupMenuItem(menu);
		return treeNodes;
	}

	public ArrayList<TreeNode> selectTheMapNode(){
		ArrayList<SearchMap> searchMapList = new ArrayList<>();
		SearchMap searchMap = new SearchMap(SearchType.TEXT, "地图");
		searchMapList.add(searchMap);
		treeNodes = TreeOperation.selectTreeNodes(tree,searchMapList);
		return treeNodes;
	}
	public ArrayList<TreeNode> selectTheMapMenu(String menu) throws Exception {
		this.selectTheMapNode();
		this.selectPopupMenuItem(menu);
		return treeNodes;
	}

	public ArrayList<TreeNode> selectMapNode(String mapName){
		ArrayList<SearchMap> searchMapList = new ArrayList<>();
		searchMapList.add(new SearchMap(SearchType.TEXT, "地图"));
		searchMapList.add(new SearchMap(SearchType.TEXT, mapName));
		treeNodes = TreeOperation.selectTreeNodes(tree,searchMapList);
		return treeNodes;
	}
	public ArrayList<TreeNode> selectMapNodeMenu(String mapName, String menu) throws Exception {
		this.selectMapNode(mapName);
		this.selectPopupMenuItem(menu);
		return treeNodes;
	}

	public ArrayList<TreeNode> selectTheDatasouceNode(){
		ArrayList<SearchMap> searchMapList = new ArrayList<>();
		SearchMap searchMap = new SearchMap(SearchType.TEXT, "数据源");
		searchMapList.add(searchMap);
		treeNodes = TreeOperation.selectTreeNodes(tree,searchMapList);
		return treeNodes;
	}
	public ArrayList<TreeNode> selectTheDatasouceNodeMenu(String menu) throws Exception {
		this.selectTheDatasouceNode();
		this.selectPopupMenuItem(menu);
		return treeNodes;
	}
	/**
	 *
	 * @param datasourceNames 数据源名称，多个数据源以“|”隔开，表示正则表达式的“或”
	 * @return
	 */
	public ArrayList<TreeNode> selectDatasourceNode(String datasourceNames){
		ArrayList<SearchMap> searchMapList = new ArrayList<SearchMap>();
		SearchMap searchMap = new SearchMap(SearchType.TEXT, "数据源");
		searchMapList.add(searchMap);
		searchMap = new SearchMap(SearchType.TEXT, datasourceNames);
		searchMapList.add(searchMap);
		treeNodes = TreeOperation.selectTreeNodes(tree,searchMapList);
		return treeNodes;
	}
	public ArrayList<TreeNode> selectDatasouceNodeMenu(String datasourceNames,String menu) throws Exception {
		this.selectDatasourceNode(datasourceNames);
		this.selectPopupMenuItem(menu);
		return treeNodes;

	}

	/**
	 *
	 * @param datasourceName 数据源名称
	 * @param datasetNames 数据集名称，多个数据集以“|”隔开，表示正则表达式的“或”
	 * @return
	 */
	public ArrayList<TreeNode> selectDatasetNode(String datasourceName, String datasetNames){
		ArrayList<SearchMap> searchMapList = new ArrayList<SearchMap>();
		SearchMap searchMap = new SearchMap(SearchType.TEXT, "数据源");
		searchMapList.add(searchMap);
		searchMap = new SearchMap(SearchType.TEXT, datasourceName);
		searchMapList.add(searchMap);
		searchMap = new SearchMap(SearchType.TEXT, datasetNames);
		searchMapList.add(searchMap);
		treeNodes = TreeOperation.selectTreeNodes(tree,searchMapList);
		return treeNodes;
	}
	public ArrayList<TreeNode> selectDatasetNodeMenu(String datasourceName, String datasetNames, String menu) throws Exception {
		this.selectDatasetNode(datasourceName, datasetNames);
		this.selectPopupMenuItem(menu);
		return treeNodes;
	}
}
